package cn.oowoo.rocketmqnative.test1;

import com.alibaba.fastjson.JSON;
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.common.consumer.ConsumeFromWhere;
import org.apache.rocketmq.common.message.MessageExt;

import java.util.List;

public class Consumer {
    public static void main(String[] args) {
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("oowoo_group");
        consumer.setNamesrvAddr("localhost:9876");//nameserv地址
        consumer.setMaxReconsumeTimes(-1);//消费重试次数，-1代表16次
        consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET);
        try {
            //订阅对应的主题topic
            consumer.subscribe("oowoo_group_test1_topic","*");
        } catch (MQClientException e) {
            e.printStackTrace();
        }
        //接收消息入口
        consumer.registerMessageListener(new MessageListenerConcurrently() {
            @Override
            public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> list, ConsumeConcurrentlyContext consumeConcurrentlyContext) {
                System.out.println("Receive MEssage: "+ JSON.toJSONString(list));
                return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
            }
        });

        try {
            //启动消费者
            consumer.start();
            System.out.println("消费者启动成功");
        } catch (MQClientException e) {
            e.printStackTrace();
        }

    }
}
